<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/30
  Time: 9:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
    <style>
        body{overflow-y: scroll;} /* 禁止刷新后出现横向滚动条 */
    </style>
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户账号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="loginName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户账号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">用户密码:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userPwd" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户密码" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">真实名称:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入真实名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户电话:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userPhone" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入电话" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">用户年龄:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userAge" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入年龄" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">身份证号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userIdCard" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入身份证号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <br>
        <label class="layui-form-label">用户地址:</label>
        <div class="layui-input-inline" style="padding: 5px">
            <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                   placeholder="请输入用户地址" style="height: 30px;border-radius: 10px">
        </div>
        <label class="layui-form-label">身份</label>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="radio" name="userStatus" value="1" title="房东">
                <input type="radio" name="userStatus" value="0" title="用户">
            </div>
        </div>

    </div>
    <div class="layui-form-item">
        <div class="layui-input-black" style="text-align: center">
            <button   class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit  lay-filter="doSearch">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->
<table id="userTable" lay-filter="userTableFilter"></table>

<script id="toolbarUser" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="userAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="delBatch">批量删除</button>
    </div>
</script>

<script id="barUser" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="resetPwd">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
    <%--<a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="selUserRole">分配角色</a>--%>
</script>



<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <input type="hidden" name="userId" value="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户账号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="loginName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户账号" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">用户密码:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userPwd" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户密码" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户姓名:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户姓名" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">用户电话:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userPhone" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户电话" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户年龄:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userAge" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户年龄" style="height: 30px;border-radius: 10px">
            </div>
            <label class="layui-form-label">用户身份证:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userIdCard" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户身份证" style="height: 30px;border-radius: 10px">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">身份:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="userStatus" value="1" checked title="房东">
                    <input type="radio" name="userStatus" value="0" title="用户">
                </div>
            </div>
            <label class="layui-form-label">用户地址:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户地址" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>
<div style="display: none" id="userDiv">
    <table id="roleTable" lay-filter="roleTableFilter"></table>
</div>
</div>


<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>


<script>
    var userTab;
    var userIndex;
    var title;
    layui.extend({
        dtree: '${pageContext.request.contextPath}/static/layui_ext/dtree/dtree'
    }).use(['form', 'layer', 'jquery','table','dtree'], function () {

        var form = layui.form, layer = layui.layer, $ = layui.jquery,table = layui.table , dtree = layui.dtree

        userTab =  table.render({
            elem:"#userTable"
            ,url:"${pageContext.request.contextPath}/user/userJson"
            ,cols:[[
                {type:'checkbox'}
                ,
                {field: 'userId', title: '用户ID'}
                ,{field: 'loginName', title: '用户账号'}
                ,{field: 'userPwd', title: '用户密码'}
                ,{field: 'userName', title: '用户姓名'}
                ,{field: 'userPhone', title: '用户电话'}
                ,{field: 'userAge', title: '用户年龄'}
                ,{field: 'userIdCard', title: '用户身份证'}
                ,{field: 'userStatus', title: '用户身份' ,templet:function(data) {
                        return data.userStatus == 1 ? "房东" :"<font color=red >用户</font>"
                    }}
                ,{field: 'address', title: '用户地址'}
                ,{fixed: 'right', title:'操作', toolbar: '#barUser',width:300}
            ]]
            ,toolbar:"#toolbarUser"
            ,page:true
            ,limits:[5,10,15]
            ,limit:5
        })

        //给查询按钮添加事件
        form.on('submit(doSearch)',function (data) {

            userTab.reload({
                where:data.field
                ,page:{curr:1}
            })

            return false;
        })

        //监听头部工具栏
        table.on('toolbar(userTableFilter)',function (obj) {
            if(obj.event=='userAdd'){
                userAdd()
            }else if(obj.event=='delBatch'){
                //获取选中的行
                var checkStatus=table.checkStatus(obj.config.id)
                if(checkStatus.data.length==0){
                    layer.msg("至少选择一行数据")
                    return false
                }
                //获取所有选中的userId
                var str=""
                for(var i=0;i<checkStatus.data.length;i++){
                    var userId=checkStatus.data[i];
                    str+=userId.userId+","
                }
                delBatch(str,checkStatus.data.length)
            }
        })

        // 监听行工具栏
        table.on('tool(userTableFilter)',function (obj) {

            if(obj.event == 'edit'){
                edit(obj.data)
            }
            if(obj.event=='del'){
                del(obj.data)
            }
            if(obj.event=='resetPwd'){
                resetPwd(obj.data)
            }
        })

        function userAdd() {
            userIndex = layer.open({
                type:1
                ,title:"用户添加"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
            })
            $('[name="userId"]').val("")
            $("#dataFrm").get(0).reset()
            title = "添加"
        }

        function edit(data){
            userIndex = layer.open({
                type:1
                ,title:"用户修改"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
            })
            //给表单赋值
            form.val('dataFrm',data)
            $('[name="userId"]').val(data.userId)
            form.render()

            title = "修改"
        }

        function del(data){
            layer.confirm("确定要删除"+data.loginName+"吗?",function (i) {
                $.post("${pageContext.request.contextPath}/user/del",{"userId":data.userId},function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(userIndex)
                            //刷新table
                            userTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }
                })
            })
        }

        function delBatch(str,count){
            layer.confirm("您确定要删除这"+count+"条数据吗？",{icon:2,title:'批量删除'},function(i){
                $.post("${pageContext.request.contextPath}/user/delBatch",{"userId":str},function(r){
                    if(r.code == 0){
                        //删除成功
                        layer.msg(r.msg, {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        }, function(){
                            // 刷新table
                            userTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        //删除失败
                        layer.msg(r.msg)
                    }
                })
            })
        }

        function resetPwd(data){
            layer.confirm("确定要重置"+data.loginName+"的密码吗?",function (i) {
                $.post("${pageContext.request.contextPath}/user/resetPwd",{"userId":data.userId},function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(userIndex)
                            //刷新table
                            userTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }
                })
            })
        }

        form.on('submit(doSubmit)',function (obj) {
            layer.confirm("您确定要"+title+"吗？",function () {
                $.post("${pageContext.request.contextPath}/user/userAddorUpd",obj.field,function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(userIndex)
                            //刷新table
                            userTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
            return false;
    })
    })
</script>
</body>
</html>
